
//
//  CHDViewController.m
//  CHDControllerScroll
//
//  Created by chd on 15-4-10.
//  Copyright (c) 2015年 chd. All rights reserved.
//
#define statusBar 20
#define Tablebar 49
#define TopScHeight 45
#define BottomScHeight [UIScreen mainScreen].bounds.size.height - TopScHeight - statusBar - Tablebar
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ButtonFont 16
#define LineHeght 3
//按钮之间的间隔
#define GapOfButton 10.0
#import "CHDViewController.h"

#import "NewsViewController.h"

#import "MusicListViewController.h"
#import "MusicPlayViewController.h"

#import "MusicDetailShareHandle.h"

@interface CHDViewController ()
{
    UIScrollView *topScroll;
    UIScrollView *bottomScroll;
    UILabel *line;
//    NSMutableArray *widthArr;
    
    UIImageView *playIV;
    
    float lastP;
    NSInteger FinalPage;
    BOOL isBtnClick;
}
@property(nonatomic,retain)NSMutableArray *widthArr;

@end

@implementation CHDViewController
- (id)init
{
    self = [super init];
    if ([super init]) {
        
        self.ScrollControllersArr = [NSMutableArray array];
        self.nameArr = [NSMutableArray array];
        self.centerArr = [NSMutableArray array];
        self.widthArr = [NSMutableArray array];
        line = [[UILabel alloc] init];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //NSLog(@"1111");
    //NSLog(@"%@",[MusicDetailShareHandle shareListDataHandle].statusMusic);
    if ([MusicDetailShareHandle shareListDataHandle].statusMusic != nil) {
        
        _titleL.text = [MusicDetailShareHandle shareListDataHandle].statusMusic.song_name;
        _subTitleL.text = [MusicDetailShareHandle shareListDataHandle].statusMusic.singer_name;
    }
    //NSLog(@"sdfsdfsdfsdf");
    
#pragma 改
    if ([TagHandle shareTagHandle].isPlaying) {
        
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_pause.png"] forState:UIControlStateNormal];
    } else {
        
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play.png"] forState:UIControlStateNormal];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUI];
    //NSLog(@"%@", self.centerArr);
    // Do any additional setup after loading the view.
    
}
- (void)setUI
{
    //NSLog(@"%d", __LINE__);
    //self.view.backgroundColor = [UIColor clearColor];
    
    if ([[UIScreen mainScreen] bounds].size.width > 375) {
        topScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBar, ScreenWidth, TopScHeight)];
    } else {
    
        topScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBar, ScreenWidth, TopScHeight)];
    }
    topScroll.showsHorizontalScrollIndicator = NO;
    topScroll.userInteractionEnabled = YES;

    topScroll.backgroundColor = [UIColor clearColor] ;

    topScroll.backgroundColor = [UIColor whiteColor] ;

    
    //根据传入字符串创建按钮，并计算contentSize
    //按钮的高与TopScroll一致
    float contenX = GapOfButton * 4;
    float scrollWidth = (ScreenWidth - contenX - GapOfButton * (_nameArr.count-1)) / _nameArr.count;
    
    UIButton *firstBT = [UIButton buttonWithType:UIButtonTypeCustom];
    [firstBT setTitle:_nameArr[0] forState:UIControlStateNormal];
    firstBT.titleLabel.font = [UIFont systemFontOfSize:ButtonFont];
    firstBT.frame = CGRectMake(contenX, 0, scrollWidth, TopScHeight - 4);
    firstBT.tag = 200;
    [firstBT addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [topScroll addSubview:firstBT];
    NSNumber *firstNum = [NSNumber numberWithFloat:firstBT.center.x];
    [self.centerArr addObject:firstNum];
    NSNumber *firstNum2 = [NSNumber numberWithFloat:firstBT.frame.size.width];
    [self.widthArr addObject:firstNum2];
    [firstBT setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    
    contenX = firstBT.frame.origin.x + scrollWidth + GapOfButton;
    
    for (int i = 1 ; i < _nameArr.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:_nameArr[i] forState:UIControlStateNormal];
        button.titleLabel.font  = [UIFont systemFontOfSize:ButtonFont];
        
        CGSize size = CGSizeMake(scrollWidth, 40);
        button.frame = CGRectMake(contenX, 0, size.width, TopScHeight - 4);
        //button.backgroundColor = [UIColor orangeColor];
        contenX = button.frame.origin.x + size.width + GapOfButton;
        button.tag = 200 + i;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topScroll addSubview:button];
        NSNumber *num = [NSNumber numberWithFloat:button.center.x];
        [self.centerArr addObject:num];
        //NSLog(@"~~~~~~~~~~~~~~~~~~~~~~~%ld",self.centerArr.count);
       // NSLog(@"~~~~~~~~~~~~~~~~~~~~~~%@",self.centerArr);
        NSNumber *num2 = [NSNumber numberWithFloat:button.frame.size.width];
        [self.widthArr addObject:num2];
        [button setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    }
    topScroll.contentSize = CGSizeMake(contenX - GapOfButton, TopScHeight);
    [self.navigationController.navigationBar addSubview:topScroll];
    NSNumber *num = [self.centerArr firstObject];
    //NSLog(@"~~~~~~~~~~~~~~~~~~%@",self.widthArr);
    NSNumber *num2 = [self.widthArr firstObject];
    line.frame = CGRectMake(num.floatValue - num2.floatValue/2, TopScHeight - LineHeght, num2.floatValue, LineHeght);
    [topScroll addSubview:line];
    line.backgroundColor = [UIColor redColor];
   // NSLog(@"%@",NSStringFromCGRect(line.frame));
    [self.view addSubview:topScroll];
    
    UIButton *sideMenuBT = [UIButton buttonWithType:UIButtonTypeCustom];
    sideMenuBT.frame = CGRectMake(0, 0, GapOfButton * 4, TopScHeight);
    //sideMenuBT.backgroundColor = [UIColor whiteColor];
    [sideMenuBT setBackgroundImage:[UIImage imageNamed:@"playlist.png"] forState:UIControlStateNormal];
    [sideMenuBT addTarget:self action:@selector(sideMenuAction:) forControlEvents:UIControlEventTouchUpInside];
    [topScroll addSubview:sideMenuBT];

    // **************************************************************************************************
    if ([[UIScreen mainScreen] bounds].size.width > 375) {
        bottomScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBar+TopScHeight, ScreenWidth, BottomScHeight)];
    } else {
        bottomScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBar+TopScHeight, ScreenWidth, BottomScHeight)];
    }
    bottomScroll.delegate = self;
    [self.view addSubview:bottomScroll];
    bottomScroll.contentSize = CGSizeMake(ScreenWidth * _ScrollControllersArr.count, BottomScHeight);
    bottomScroll.pagingEnabled = YES;
    bottomScroll.clipsToBounds = YES;
    bottomScroll.bounces = NO;
    
    int j = 0 ;
    for (UIViewController *controller in _ScrollControllersArr) {
        controller.view.frame = CGRectMake(ScreenWidth * j , 0, ScreenWidth, BottomScHeight);
        //UIColor *bg_color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroud_hudie2.jpg"]];
        //controller.view.backgroundColor = bg_color;
        //NSLog(@"%@",NSStringFromCGRect(controller.view.frame));
        [bottomScroll addSubview:controller.view];
        
        
        j++;
    }
    
    // ***************************************************************************************************
#pragma 改动
    
    UILabel *tableBar = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenHeight - Tablebar, ScreenWidth, Tablebar)];
    tableBar.text = @"tableBar";
    tableBar.backgroundColor = [UIColor whiteColor];
    tableBar.textAlignment = NSTextAlignmentCenter;
    tableBar.textColor = [UIColor whiteColor];
    tableBar.userInteractionEnabled = YES;
    [self.view addSubview:tableBar];
    
    UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //listButton.backgroundColor = [UIColor redColor];
    [listButton setBackgroundImage:[UIImage imageNamed:@"list_menu_playlist.png"] forState:UIControlStateNormal];
    listButton.frame = CGRectMake(ScreenWidth - tableBar.frame.size.height + 10, 10, 30, 30);
    [listButton addTarget:self action:@selector(listButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [tableBar addSubview:listButton];
    
    playIV = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, tableBar.frame.origin.y, tableBar.frame.size.height, tableBar.frame.size.height)];
    //playIV.backgroundColor = [UIColor yellowColor];
    playIV.image = [UIImage imageNamed:@"musiccircle_userinfo_avatar_default.png"];
    //NSString *urlStr = [MusicDetailShareHandle shareListDataHandle].playingMusic;
    playIV.userInteractionEnabled = YES;
    [self.view addSubview:playIV];
    [playIV release];
    
    self.titleL = [[UILabel alloc] initWithFrame:CGRectMake(playIV.frame.size.width + 5, playIV.frame.origin.y, 150, tableBar.frame.size.height / 3 + 10)];
    //_titleL.backgroundColor = [UIColor yellowColor];
    _titleL.font = [UIFont systemFontOfSize:14.0];
    _titleL.textColor = [UIColor darkGrayColor];
    [self.view addSubview:_titleL];
    [_titleL release];
    
    self.subTitleL = [[UILabel alloc] initWithFrame:CGRectMake(_titleL.frame.origin.x, _titleL.frame.origin.y + _titleL.frame.size.height, _titleL.frame.size.width, _titleL.frame.size.height / 2)];
    //_subTitleL.backgroundColor = [UIColor cyanColor];
    _subTitleL.font = [UIFont systemFontOfSize:12.0];
    _subTitleL.textColor = [UIColor grayColor];
    [self.view addSubview:_subTitleL];
    [_subTitleL release];
    /*
    self.duringL = [[UILabel alloc] initWithFrame:CGRectMake(_titleL.frame.origin.x, _subTitleL.frame.origin.y + _subTitleL.frame.size.height, _titleL.frame.size.width, _subTitleL.frame.size.height)];
    //_duringL.backgroundColor = [UIColor redColor];
    _duringL.font = [UIFont systemFontOfSize:10.0];
    _duringL.textColor = [UIColor grayColor];
    [self.view addSubview:_duringL];
    [_duringL release];*/
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //playButton.tag = 10001;
    [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play.png"] forState:UIControlStateNormal];
    if ([[UIScreen mainScreen] bounds].size.width == 375) {
        
        self.playButton.frame = CGRectMake(_titleL.frame.origin.x + _titleL.frame.size.width + 40, _titleL.frame.origin.y, tableBar.frame.size.height, tableBar.frame.size.height);
        
    } else if ([[UIScreen mainScreen] bounds].size.width > 375) {
    
        self.playButton.frame = CGRectMake(_titleL.frame.origin.x + _titleL.frame.size.width + 60, _titleL.frame.origin.y, tableBar.frame.size.height, tableBar.frame.size.height);
    } else {
    
        self.playButton.frame = CGRectMake(_titleL.frame.origin.x + _titleL.frame.size.width, _titleL.frame.origin.y + 5, tableBar.frame.size.height - 10, tableBar.frame.size.height - 10);
    }
    [self.playButton addTarget:self action:@selector(playingAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.playButton];
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.tag = 10002;
    [self.nextButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play_next.png"] forState:UIControlStateNormal];
    if ([[UIScreen mainScreen] bounds].size.width >= 375) {
        
        self.nextButton.frame = CGRectMake(self.playButton.frame.origin.x + self.playButton.frame.size.width, self.playButton.frame.origin.y, self.playButton.frame.size.width, self.playButton.frame.size.height);
        
    } else {
    
        self.nextButton.frame = CGRectMake(self.playButton.frame.origin.x + self.playButton.frame.size.width, self.playButton.frame.origin.y, tableBar.frame.size.height - 10, tableBar.frame.size.height - 10);
    }
    [self.view addSubview:self.nextButton];
    
    [self.nextButton addTarget:self action:@selector(playingAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [playIV addGestureRecognizer:imageTap];
    [imageTap release];
    
}

-(void)sideMenuAction:(UIButton *)button
{
    [self.myDelegation presentTo];
}

-(void)playingAction:(UIButton *)button
{
    if (self.playButton == button) {
        
        [TagHandle shareTagHandle].isPlaying = ![TagHandle shareTagHandle].isPlaying;
        if ([TagHandle shareTagHandle].isPlaying) {
#pragma 改
            if ([Player shareDataHandle].oldIndex != [Player shareDataHandle].playingIndex) {
                
                [[Player shareDataHandle] PlayForArray];
            } else {
                [[Player shareDataHandle].player resume];
            }
            [self.playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_pause.png"] forState:UIControlStateNormal];
            
        } else {
            
            [[Player shareDataHandle].player pause];
            [self.playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play.png"] forState:UIControlStateNormal];
        }
    } else if (self.nextButton == button) {
        if (FinalPage == 1) {
            [Player nextPlayRadio];
        }else{
    
            [[Player shareDataHandle] NextPlay];
        
            _titleL.text = [MusicDetailShareHandle shareListDataHandle].statusMusic.song_name;
            _subTitleL.text = [MusicDetailShareHandle shareListDataHandle].statusMusic.singer_name;
        }

    }
}

-(void)listButtonAction
{
    //if (FinalPage == 0) {
        
        MusicListViewController *musicListVC = [[MusicListViewController alloc] init];
        
        [self presentViewController:musicListVC animated:YES completion:^{
            
            
        }];
        
    //}
    /*if (FinalPage == 1) {
        
        SongListViewController *songListVC = [[SongListViewController alloc]init];
        songListVC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收起" style:UIBarButtonItemStylePlain target:self action:@selector(rightButton)];
    
        UINavigationController *songListC = [[UINavigationController alloc]initWithRootViewController:songListVC];
    
        UITabBarController *tabBar = [[UITabBarController alloc]init];
        tabBar.viewControllers = [NSMutableArray arrayWithObject:songListC];
        
        [self presentViewController:tabBar animated:YES completion:^{
            
            
        }];
    }*/
}
-(void)rightButton
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

-(void)tapAction:(UITapGestureRecognizer *)tapGesture
{
    //if (FinalPage == 0) {
        
        MusicPlayViewController *musicPlayVC = [[MusicPlayViewController alloc] init];
        
        UINavigationController *playNC = [[UINavigationController alloc] initWithRootViewController:musicPlayVC];
        
        playNC.navigationController.navigationBar.translucent = NO;
        //[self.navigationController pushViewController:playNC animated:YES];
        [self presentViewController:playNC animated:YES completion:^{
            
            
        }];
    //}
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    //NSLog(@"%d", __LINE__);
    //NSLog(@"bbbbbb%@", self.centerArr);
    if (!isBtnClick) {
        float needChange = 0;
        int a =0;
        
        
        for (int i =0;i<self.centerArr.count;i++) {
            //NSLog(@"aaaaaaaaa%d", i);
            NSNumber *num = self.centerArr[i];
            
            if (num.floatValue >= line.center.x) {
                //NSLog(@"num--%f  line ---%f",num.floatValue,line.center.x);
                
                if (i-1 >= 0) {
                    needChange = [self.centerArr[i] floatValue] - [self.centerArr[i-1] floatValue];
                    //NSLog(@"%f",needChange);
                    a = i;
                    break;
                    
                }
                
            }
        }
        
        float offset = 0;
        offset = needChange/ScreenWidth * (scrollView.contentOffset.x - (a - 1) * ScreenWidth);
        if (a != 0) {
            line.center = CGPointMake([self.centerArr[a -1] floatValue] +  offset, TopScHeight - LineHeght/2.0);
        }

        //NSLog(@"====%@",self.widthArr);
        line.bounds = CGRectMake(0, 0, [self.widthArr[a -1] floatValue] + ([self.widthArr[a] floatValue] - [self.widthArr[a - 1] floatValue])/ScreenWidth *(scrollView.contentOffset.x - (a -1) *ScreenWidth), LineHeght);
        lastP = scrollView.contentOffset.x;
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
    
    if (!decelerate) {
        FinalPage = (NSInteger)scrollView.contentOffset.x/ScreenWidth;
        [self changeTop];
    }
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    FinalPage = (NSInteger)scrollView.contentOffset.x/ScreenWidth;
    [self changeTop];
}
- (void)changeTop
{
    isBtnClick = NO;
    float x = [self.centerArr[FinalPage] floatValue];
    
    [UIView animateWithDuration:0.3 animations:^{
        if (x>ScreenWidth/2.0 && x<topScroll.contentSize.width - ScreenWidth/2.0) {
            topScroll.contentOffset = CGPointMake(x-ScreenWidth/2.0, 0);
        }
        else if (x >= topScroll.contentSize.width - ScreenWidth/2.0) {
            topScroll.contentOffset = CGPointMake(topScroll.contentSize.width - ScreenWidth, 0);
        }
        else{
            topScroll.contentOffset = CGPointMake(0, 0);
        }
        
    }];
}
- (void)btnClick:(UIButton *)button
{
    isBtnClick = YES;
    FinalPage = button.tag - 200;
    [self changeBottom];
    [self changeTop];
    //[self changeLine];
    //[self changeLineWithBtn:button];
    [self changeLine];
    
}
- (void)changeLineWithBtn:(UIButton *)btn
{
    [UIView animateWithDuration:0.3 animations:^{
        line.bounds = CGRectMake(0, 0, btn.frame.size.width, LineHeght);
        line.center = CGPointMake(btn.center.x, TopScHeight - LineHeght/2.0);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)changeLine
{
    [UIView animateWithDuration:0.3 animations:^{
//        line.center = CGPointMake([self.centerArr[FinalPage] floatValue], TopScHeight - LineHeght/2.0);
//        line.bounds = CGRectMake(0, 0, [widthArr[FinalPage] floatValue], LineHeght);

        //NSLog(@"+++++++++%@",self.centerArr);
        line.frame = CGRectMake([self.centerArr[FinalPage] floatValue] -[self.widthArr[FinalPage] floatValue]/2.0, TopScHeight - LineHeght, [self.widthArr[FinalPage] floatValue], LineHeght);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)changeBottom
{
    [UIView animateWithDuration:0.3 animations:^{
        bottomScroll.contentOffset = CGPointMake(ScreenWidth *FinalPage, 0);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
